package com.android.jianboshi.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.jianboshi.R;
import com.android.jianboshi.constants.SPConstants;
import com.android.jianboshi.dialog.XieYi_DialogFragment;
import com.android.jianboshi.utils.LocaleUtils;
import com.android.jianboshi.manager.LanguageManager;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/21.
 */

public class LoadingActivity extends BaseActivity {

//    @BindView(R.id.tv_tips)
//    TextView tv_tips;
    @BindView(R.id.iv_img1)
    ImageView iv_img1;

    Handler handler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_loading;
    }

    @Override
    protected boolean isImmersion() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        LocaleUtils.changeAppLanguage(this, LanguageManager.getCurrentLocale());
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {

//        tv_tips.setText(getString(R.string.loading_text));

        boolean hasOk = SPUtil.getBoolValue(mContext, SPConstants.HasAccept, false);
        if (hasOk) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    goToMain();
                }
            }, 2000);
        } else {
            XieYi_DialogFragment dialogFragment = (XieYi_DialogFragment) XieYi_DialogFragment.showDialog(getSupportFragmentManager(), null);
            dialogFragment.setCallback(new CommCallBack() {
                @Override
                public void onResult(Object obj) {
                    boolean result = (boolean) obj;
                    if (!result) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("isSecond", true);
                        XieYi_DialogFragment dialogFragment1 = (XieYi_DialogFragment) XieYi_DialogFragment.showDialog(getSupportFragmentManager(), params);
                        dialogFragment1.setCallback(new CommCallBack() {
                            @Override
                            public void onResult(Object obj) {
                                boolean result = (boolean) obj;
                                if (result) {
                                    SPUtil.putValue(mContext, SPConstants.HasAccept, true);
                                    goToMain();
                                }
                            }
                        });
                    } else {
                        SPUtil.putValue(mContext, SPConstants.HasAccept, true);
                        goToMain();
                    }
                }
            });
        }
    }

    @Override
    protected void initData() {
        Locale locale = LanguageManager.getCurrentLocale();
        int resource = 0;
        if (locale.getLanguage().equals(Locale.CHINESE.getLanguage())) {
            resource = R.drawable.ico_loading_zh;
        } else if (locale.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
            resource = R.drawable.ico_loading_en;
        } else if (locale.getLanguage().equals(new Locale("es").getLanguage())) {
            resource = R.drawable.ico_loading_es;
        } else if (locale.getLanguage().equals(Locale.FRENCH.getLanguage())) {
            resource = R.drawable.ico_loading_fr;
        } else if (locale.getLanguage().equals(Locale.GERMAN.getLanguage())) {
            resource = R.drawable.ico_loading_de;
        } else if (locale.getLanguage().equals(new Locale("ru").getLanguage())) {
            resource = R.drawable.ico_loading_ru;
        } else if (locale.getLanguage().equals(Locale.JAPANESE.getLanguage())) {
            resource = R.drawable.ico_loading_ja;
        } else if (locale.getLanguage().equals(new Locale("pt").getLanguage())) {
            resource = R.drawable.ico_loading_pt;
        } else {
            resource = R.drawable.ico_loading_en;
        }
        iv_img1.setImageResource(resource);
    }

    //跳首页
    private void goToMain() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        Intent intent;
//        if (UserManager.isLogin(mContext)) {
//            intent = new Intent(mContext, MainActivity.class);
//        } else {
//            intent = new Intent(mContext, LoginActivity.class);
//        }
        intent = new Intent(mContext, MainActivity.class);
        mContext.startActivity(intent);
        overridePendingTransition(R.anim.main_activity_in, R.anim.splash_out);
        finish();
    }


}
